package com.coffee.admin.controller;


import com.coffee.core.result.Result;
import com.coffee.job.api.quartz.api.QuartzJobService;
import com.coffee.job.api.quartz.entity.TaskDefine;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @author Xulg
 * Created in 2019-08-07 9:21
 */

@RestController
@RequestMapping("quartz")
@Validated
public class QuartzJobManageController {
    private static final Logger logger = LoggerFactory.getLogger(QuartzJobManageController.class);

    @Reference(check = false)
    private QuartzJobService quartzJobService;

    /**
     * 创建和启动定时任务
     *
     * @param name           定时任务的名字
     * @param group          定时任务的组名
     * @param description    定时任务的描述
     * @param cronExpression 定时任务的执行cron
     * @param jobClassName   定时任务的具体执行逻辑类全路径名称
     * @return the result
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "addScheduleJob", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<Void> addScheduleJob(@NotEmpty(message = "定时任务的名字坚决不为空") String name,
                                       @NotEmpty(message = "定时任务的组名坚决不为空") String group,
                                       @NotEmpty(message = "定时任务的执行cron不能为空") String cronExpression,
                                       @NotEmpty(message = "定时任务的具体执行逻辑类坚决不能为空") String jobClassName,
                                       @NotEmpty(message = "定时任务的描述不能为空") String description) {

        Void result = quartzJobService.addScheduleJob(name, group, cronExpression, jobClassName, description);
        return Result.success(result);


    }

    /**
     * 暂停Job
     *
     * @param name  定时任务的名字
     * @param group 定时任务的组名
     * @return the result
     */
    @RequestMapping(value = "pauseJob", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<Void> pauseJob(@NotEmpty(message = "定时任务的名字坚决不为空") String name,
                                 @NotEmpty(message = "定时任务的组名坚决不为空") String group) {
        return Result.success(quartzJobService.pauseJob(name, group));
    }

    /**
     * 恢复Job
     *
     * @param name  定时任务的名字
     * @param group 定时任务的组名
     * @return the result
     */
    @RequestMapping(value = "resumeJob", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<Void> resumeJob(@NotEmpty(message = "定时任务的名字坚决不为空") String name,
                                  @NotEmpty(message = "定时任务的组名坚决不为空") String group) {
        return Result.success(quartzJobService.resumeJob(name, group));
    }

    /**
     * 删除Job
     *
     * @param name  定时任务的名字
     * @param group 定时任务的组名
     * @return the result
     */
    @RequestMapping(value = "deleteJob", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<Void> deleteJob(@NotEmpty(message = "定时任务的名字坚决不为空") String name,
                                  @NotEmpty(message = "定时任务的组名坚决不为空") String group) {
        return Result.success(quartzJobService.deleteJob(name, group));
    }

    /**
     * 修改Job的cron表达式
     */
    @RequestMapping(value = "modifyJobCron", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<Boolean> modifyJobCron(@NotEmpty(message = "定时任务的名字坚决不为空") String name,
                                         @NotEmpty(message = "定时任务的组名坚决不为空") String group,
                                         @NotEmpty(message = "定时任务的cron表达式坚决不为空") String cronExpression) {
        return Result.success(quartzJobService.modifyJobCron(name, group, cronExpression));
    }

    /**
     * 查询所有的job
     *
     * @return the result
     */
    @RequestMapping(value = "queryAllJob", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<TaskDefine>> queryAllJob() {
        return Result.success(quartzJobService.queryAllJob());
    }

    /**
     * 查询所有正在运行的job
     *
     * @return the result
     */
    @RequestMapping(value = "queryAllCurrentRunningJob", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<List<TaskDefine>> queryAllCurrentRunningJob() {
        return Result.success(quartzJobService.queryAllCurrentRunningJob());
    }
}